<?

class mysqlInterval{
	//----------------------------------------------//
	//	RECURSIVITE INTERVALLAIRE		//
	//----------------------------------------------//

	//Creation des tables d intervalle
	function createIntervalIndex(){
		//ON cree donc une table contenant les bords droits , bords gauche ainsi que l association
		//avec l id de la donnï¿œe
		$sql  = 'CREATE TABLE IF NOT EXISTS `'.$this->Prefix.$this->titre.'-Interval` (';
		$sql.= '`Id` INT NOT NULL ,';
		$sql.= '`Bd` INT NOT NULL ,';
		$sql.= '`Bg` INT NOT NULL )';
		mysqlDriver::executeSql($sql,"CREATE");
		mysqlInterval::purgeInterval();
		mysqlInterval::IndexInterval(0,1);
	}

	//Vide la table d intervalles
	function purgeInterval(){
		$sql='TRUNCATE TABLE `'.$this->Prefix.$this->titre.'-Interval`';
		mysqlDriver::executeSql($sql,"DELETE");
	}

	//Fonction de reindexation du systeme d intervalle a partir de l association parente
	function IndexInterval($Id,$Bg){
		//On commence par enregistrer le bord gauche
		//----> Bord Gauche ok
		$BordGauche = $Bg;
		$Bg++;
		$Objs=mysqlInterval::getChildForInterval($Id);
		if (is_array($Objs))foreach ($Objs as $Obj){
			//Pour chaque reponse on relance la methode pour mettre a jour toute la table
			$Bg=mysqlInterval::IndexInterval($Obj["Id"],$Bg,$Total);
			$Bg++;
		}
		//On enregistre le bord droit
		mysqlInterval::recordInterval($Id,$BordGauche,$Bg);
		return $Bg;
	}

	//Insere un nouvel index
	function insertIntervalData($Id,$IdParent){
		//Recuperation des informations du parent pour avoir le bord droit
		if ($IdParent=="") $IdParent = 0;
		$sql='SELECT Bd FROM `'.$this->Prefix.$this->titre.'-Interval` WHERE Id='.$IdParent;
		$Result = mysqlDriver::executeSql($sql,"SELECT_SYS");
		$Bd = $Result[0]["Bd"];
		//ON pousse les bornes droite de 2
		$sql = 'UPDATE `'.$this->Prefix.$this->titre.'-Interval` SET Bd=Bd+2 WHERE Bd >= '.$Bd;
		mysqlDriver::executeSql($sql,"SELECT_SYS");
		//On pousse egalement le bord gauche de 2
		$sql = 'UPDATE `'.$this->Prefix.$this->titre.'-Interval` SET Bg=Bg+2 WHERE Bg >= '.$Bd;
		mysqlDriver::executeSql($sql,"SELECT_SYS");
		//ON insere enfin l index
		$sql = 'INSERT INTO `'.$this->Prefix.$this->titre.'-Interval` (Bg,Id,Bd) VALUES ('.$Bd.','.$Id.','.($Bd+1).')';
		mysqlDriver::executeSql($sql,"SELECT_SYS");
	}

	//Eneleve un index
	function removeIntervalData($Id){
		//Recuperation des informations du parent pour avoir le bord droit
		$sql='SELECT Bg FROM `'.$this->Prefix.$this->titre.'-Interval` WHERE Id='.$Id;
		$Result = mysqlDriver::executeSql($sql,"SELECT_SYS");
		$Bg = $Result[0]["Bg"];
		//ON supprime les index
		$sql = 'DELETE FROM `'.$this->Prefix.$this->titre.'-Interval` WHERE Id='.$Id;
		mysqlDriver::executeSql($sql,"SELECT_SYS");
		//ON pousse les bornes droite de 2
		$sql = 'UPDATE `'.$this->Prefix.$this->titre.'-Interval` SET Bg=Bg-2 WHERE Bg >= '.$Bg;
		mysqlDriver::executeSql($sql,"SELECT_SYS");
		//On pousse egalement le bord gauche de 2
		$sql = 'UPDATE `'.$this->Prefix.$this->titre.'-Interval` SET Bd=Bd-2 WHERE Bd >= '.$Bg;
		mysqlDriver::executeSql($sql,"SELECT_SYS");
	}

	//Recupere l ensemble des enfants d une donnee
	function getChildForInterval($Id) {
		//On recherche recursivement les categories et on recree l index selon les cardinalites
		foreach ($this->childOf as $Child){
			if ($Child["Titre"]==$this->titre) {
				$Key = $Child;
				$Card = $Child["Card"];
			}
		}
		if ($Card=="0,1"||$Card=="1,1"){
			$Champ = $this->findReflexive();
			//On appelle la table concernï¿œe
			$Data['Table'][0]['Alias'] = "m";
			$Data['Table'][0]['Nom'] = $this->titre;
			$Data['Select'][0]['Nom'] = "m.Id";
			if ($Id==0) {
				$Data['Groupe'][0]['Lien'] = "OR";
				$Data['Groupe'][0]['Condition'][0] = "m.$Champ IS NULL";
				$Data['Groupe'][0]['Condition'][1] = "m.$Champ=0";
			}else{
				$Data['Groupe'][0]['Lien'] = "AND";
				$Data['Groupe'][0]['Condition'][0] = "m.$Champ=$Id";
			}
		}elseif ($Card=="0,n"||$Card=="1,n"){
			//Il faut donc faire une jointure avec la table associative
			//On appelle la table concernï¿œe
			$Data['Table'][0]['Alias'] = "m";
			$Data['Table'][0]['Nom'] = $this->titre;
			$Data['Table'][1]['Alias'] = "j";
			$Data['Table'][1]['Nom'] = $Key["Table"];
			$Data['Select'][0]['Nom'] = "m.Id";
			$Data['Groupe'][0]['Lien'] = "AND";
			if ($Id==0) {
				$Data['Groupe'][0]['Condition'][0] = "j.".$Key["Champ"]." IS NULL";
			}else{
				$Data['Groupe'][0]['Condition'][0] = "j.".$Key["Champ"]."='$Id'";
			}
			$Data['Groupe'][0]['Condition'][0] = "j.".$Key["Champ"]."='$Id'";
			$Data['Groupe'][0]['Condition'][1] = "m.Id=j.".$Key["Titre"];
		}
		$sql = mysqlFunctions::createSql("SELECT_INTERVAL",$Data,$Analyse);
		$resql = mysqlDriver::executeSql($sql,"SELECT_SYS");
		//if (MDB2::isError($resql)) return false;
		$i = 0;
		foreach($resql as $tab){
			$Results[$i]=$tab;
			$Results[$i]["ObjectType"]=$this->titre;
			$i++;
		}
		return $Results;
	}

	//Enregistre d un index d intervalle
	function recordInterval($Id,$Bg,$Bd){
		$sql  = 'INSERT INTO `'.$this->Prefix.$this->titre.'-Interval` SET ';
		if ($Bg!="") $sql .= ' Bg = "'.$Bg.'",';
		if ($Bd!="") $sql .= ' Bd = "'.$Bd.'" ,';
		$sql .= 'Id="'.$Id.'" ';
		mysqlDriver::executeSql($sql,"UPDATE");
	}

	function verifyIntervalIndex() {return false;}


}
